package com.indipendent.prototypeone.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.independent.prototypeone.entity.User;
import com.indipendent.prototypeone.services.LoginServiceDelegate;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = 1876366710321874049L;

	private User user;

	@Autowired
	private LoginServiceDelegate loginServiceDelegate;

	@Override
	public String execute() {

		User userById = loginServiceDelegate.getUserById(user);
		if (null == userById || "".equals(userById.getUsername())) {
			return ERROR;
		}

		return SUCCESS;
	}

	public void validate() {
		if ("".equals(user.getUsername()) || "".equals(user.getPassword())) {
			addFieldError("username", getText("E' obbligatorio inserire entrambi i campi"));
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public LoginServiceDelegate getLoginServiceDelegate() {
		return loginServiceDelegate;
	}

	public void setLoginServiceDelegate(LoginServiceDelegate loginServiceDelegate) {
		this.loginServiceDelegate = loginServiceDelegate;
	}

}